﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Card.Common;
using System.Data;

namespace Card.Web
{
    public partial class OnlinePrint : BasePage
    {
        #region
        private int m_CardId = 0;
        /// <summary>
        /// 名片ID
        /// </summary>
        private int CardID
        {
            get
            {
                if (m_CardId <= 0)
                {
                    if (Request.QueryString["id"] != null && int.TryParse(Request.QueryString["id"], out m_CardId))
                    {
                        return m_CardId;
                    }

                }
                return m_CardId;
            }
        }

        private DataTable dtCard = null;
        private DataTable dtPrice = null;
        private DataTable dtCorner = null;


        private Entity.CardSetting m_DefaultCard = null;

        public Entity.CardSetting DefaultCard
        {
            get
            {
                if (m_DefaultCard == null)
                {
                    m_DefaultCard = new Card.Entity.CardSetting()
                                        {
                                            CardCode = dtCard.Rows[0]["CARD_CODE"].ToString().Trim(),
                                            Area = dtCard.Rows[0]["AREA"].ToString().Trim(),
                                            CardName = dtCard.Rows[0]["CARD_NAME"].ToString().Trim(),
                                            Sort = string.IsNullOrEmpty(dtCard.Rows[0]["SORT"].ToString()) ? 0 : int.Parse(dtCard.Rows[0]["SORT"].ToString()),
                                            Decription = dtCard.Rows[0]["DECRIPTION"].ToString().Trim(),
                                            IsDefault = true
                                        };
                }

                return m_DefaultCard;
            }
        }

        private Entity.CardPriceSetting m_DefaultPrice = null;

        public Entity.CardPriceSetting DefaultPrice
        {
            get
            {
                if (m_DefaultPrice == null)
                {
                    var rows = (from row in dtPrice.AsEnumerable()
                                where string.Compare(row["CARD_CODE"].ToString(), DefaultCard.CardCode, true) == 0
                                    && int.Parse(row["NUM"].ToString()) == 2
                                select row).ToArray();

                    if (rows != null && rows.Length > 0)
                    {
                        m_DefaultPrice = new Card.Entity.CardPriceSetting()
                        {
                            CardCode = rows[0]["CARD_CODE"].ToString().Trim(),
                            Area = rows[0]["AREA"].ToString().Trim(),
                            Num = int.Parse(rows[0]["NUM"].ToString().Trim()),
                            Sort = string.IsNullOrEmpty(rows[0]["SORT"].ToString()) ? 0 : int.Parse(rows[0]["SORT"].ToString()),
                            Price = decimal.Parse(rows[0]["PRICE"].ToString().Trim()),
                            IsDefault = true
                        };
                    }
                }
                return m_DefaultPrice;
            }
        }

        private Entity.CornerSetting m_DefaultCorner = null;

        public Entity.CornerSetting DefaultCorner
        {
            get {
                if (m_DefaultCorner == null)
                {
                    m_DefaultCorner = new Card.Entity.CornerSetting()
                    {
                        CornerCode = dtCorner.Rows[0]["CORNER_CODE"].ToString().Trim(),
                        Area = dtCorner.Rows[0]["AREA"].ToString().Trim(),
                        CornerName = dtCorner.Rows[0]["CORNER_NAME"].ToString().Trim(),
                        Sort = string.IsNullOrEmpty(dtCorner.Rows[0]["SORT"].ToString()) ? 0 : int.Parse(dtCorner.Rows[0]["SORT"].ToString()),
                        Price = decimal.Parse(dtCorner.Rows[0]["PRICE"].ToString().Trim()),
                        IsDefault = true
                    };
                }
                
                return m_DefaultCorner; }
        }
        #endregion

        #region
        protected void Page_Load(object sender, EventArgs e)
        {
            this.DoInit();
        }
        #endregion

        #region
        private void DoInit()
        {
            Entity.UserCard card = BLL.UserCardManager.GetEntity(this.CardID, this.Passport);
            if (card != null && this.Passport.UserID == card.UserID)
            {
                if (string.Compare(card.Style, CONST.TEMPLATE_STYLE.VERTICAL, true) == 0)
                {
                    this.card_ctn.Attributes.Add("class", "vertical prin_zmian");
                }
                else
                {
                    this.card_ctn.Attributes.Add("class", "horizontal prin_zmian");
                }
                this.ltlCardName.Text = card.TemplateName;
                this.frontImg.ImageUrl = Card.Utility.ImageUtilty.GetUserCardThumbImagePath(this.CardID, this.Passport.UserID, CONST.CARD_FACE_TYPE.FRONT, card.FrontImage);
                this.backImg.ImageUrl = Card.Utility.ImageUtilty.GetUserCardThumbImagePath(this.CardID, this.Passport.UserID, CONST.CARD_FACE_TYPE.BACK, card.BackImage);

                this.dtCard = SettingCache.DtCardSetting;
                this.dtCorner = SettingCache.DtCornerSetting;
                this.dtPrice = SettingCache.DtPriceSetting;

                this.hnd_corner.Value = this.DefaultCorner.CornerCode;                
                this.hnd_paper.Value = this.DefaultCard.CardCode;
                this.hnd_num.Value = this.DefaultPrice.Num.ToString();
                this.hndCardID.Value = card.ID.ToString();
                this.hndCardType.Value = CONST.CARD_TYPE.USER_CARD;
                BAF.Entity.LookupValue lookUp = LookUpCache.GetLookupValue(CONST.LOOKUP_TYPE.SYSTEM_CONFIG, "ADJUST_PRICE");
                if (lookUp != null)
                {
                    this.ltlAdjustPrice.Text = lookUp.Value;
                    this.hndAdjustPrice.Value = lookUp.Value;
                    this.ltlAdjustPrice1.Text = lookUp.Value;
                }
                this.rptParper.DataSource = SettingCache.DtCardSetting;
                this.rptParper.DataBind();

                this.rptCorner.DataSource = SettingCache.DtCornerSetting;
                this.rptCorner.DataBind();

                this.rptNumber.DataSource = (SettingCache.DtPriceSetting.Select(string.Format("CARD_CODE='{0}'", this.DefaultCard.CardCode))).CopyToDataTable();
                this.rptNumber.DataBind();
            }
            else
            {
                Response.Redirect("/");
            }

        }

        public string GetPaperPrice(string paper)
        {
            return string.Format("{{{0}}}", string.Join(",", (from row in SettingCache.DtPriceSetting.AsEnumerable()
                                                              where string.Compare(row["CARD_CODE"].ToString(), paper, true) == 0
                                                              select string.Format("'{0}':{1}", row["NUM"].ToString(), row["PRICE"].ToString())).ToArray()));
        }
        #endregion
    }
}
